Angewandte Datenverarbeitung und Visualisierung (WiSe23/24)
  • D. Palleschi
  • Download PDF
  • Download ePub
  1. Overview
  2. 1  Setting up R and RStudio
  • Overview
    • Course Overview
    • Syllabus
    • Kursübersicht
    • 1  Setting up R and RStudio
  • Course mats
    • 2  Einführung in R und RStudio
  • Reports
    • Bericht 1
    • Bericht 2
    • Bericht 3
  • References

Overview

  • 2 R and RStudio
    • 2.1 Packages
    • 2.2 Developer packages (optional)
    • 2.3 RStudio Global options (optional)
  • 3 tinyTex
  • 4 webR
  • 5 timesaveR
  • 6 glossary

1  Setting up R and RStudio

Author
Affiliation

Daniela Palleschi

Humboldt-Universität zu Berlin

Published

April 12, 2023

This document outlines the steps required to set up our reproducible workflow. Chapter 2 provides an overview of installing R, RStudio, and required packages. These steps are required. Chapter 3 describes how to install TinyTex, which is required to render LaTeX-style documents (i.e., PDFs) in R.

?sec-zotero provides tips on installing Zotero and related addins for integrations with R and RStudio. This is optional, and slightly more advanced.

2 R and RStudio

Download R here.

Download RStudio here.

You must first download R, then RStudio.

2.1 Packages

R packages available on the Comprehensive R Archive Network, commonly known as CRAN repository, can be easily installed using the command isntall.packages("packageName"). Some packages we will be needing are:

  • here package
  • tidyverse family of packages
    • will automatically include packages we need, like dplyr and ggplot2

To download multiple packages at once, use the ‘concatenate’ function in r (c()) inside install.packages():

install.packages(c("here", 
                   "tidyverse",
                   "pacman"))

2.2 Developer packages (optional)

Not all packages are available on CRAN. You will need to install them directly through the developer. Two packages that are commonly used to do this are remotes and devools. Let’s try installing remotes and then installing the rbbt, an addin for Zotero (discussed below).

if(!requireNamespace("remotes", quietly = TRUE)) install.packages("remotes")
remotes::install_github("paleolimbot/rbbt")

2.2.1 kableExtra for table formatting

remotes::install_github("haozhu233/kableExtra")

2.2.2 quartoExtra

For dark/light mode toggle: https://debruine.github.io/quarto_demo/quartoExtra.html

devtools::install_github("debruine/quartoExtra")

2.3 RStudio Global options (optional)

Here are my preferred global options (RStudio > Tools > Global options). I highly recommend following the settings for ‘workspace’ and ‘R Sessions’ to ensure reproducibility. The other settings you can play around with to find what you like.

  • General > Basic
    • Workspace (for reproducibile workflow!!!)
      • uncheck ‘Restore .RData into workshapce at startup’
      • Save workspace to .RData on exit: Never
    • R Sessions
      • uncheck ‘Restore previously open source documents at startup’
  • Code > Display
    • General
      • Show white space characters
      • Allow scroll past end of document
      • highlight selected line
  • Appearance
    • Editor theme: Cobalt

3 tinyTex

To render PDF documents with LaTeX under the hood, we need to install tinytex. There are different ways to do this:

  • run the following in the terminal: quarto install tinytex
  • or in the console: tinytex::install_tinytex()

4 webR

In the Terminal:

quarto add coatless/quarto-webr

Then add WebR filter to slides in the YAML:

format: revealjs
filters:
  - webr

5 timesaveR

e.g., for the as_tribble() function

remotes::install_github('lukaswallrich/timesaveR')

6 glossary

https://debruine.github.io/glossary/

install.packages("glossary")
Kursübersicht
2  Einführung in R und RStudio
Source Code
---
title: "Setting up R and RStudio"
author: "Daniela Palleschi"
institute: Humboldt-Universität zu Berlin
lang: en
date: 2023-04-12
format:
  html:
    output-file: sheet-setup.html
    toc: true
    toc-depth: 2
    toc-title: 'Overview'
    code-link: true
    code-overflow: wrap
    code-tools: true
    number-sections: true
    self-contained: true
  pdf:
    output-file: pdf-setup.pdf
    toc: true
    number-sections: false
    colorlinks: true
    code-overflow: wrap
editor_options: 
  chunk_output_type: console
---

```{r, echo = F}
knitr::opts_chunk$set(eval = F, # evaluate = T for REPRODUCIBLE analyses
                      echo = T, # 'print code chunk?'
                      message = F, # print messages?
                      error = F, # render even if errors encountered?
                      warning = F) # print warnings?
```

This document outlines the steps required to set up our reproducible workflow. @sec-R provides an overview of installing R, RStudio, and required packages. These steps are required. @sec-tinytex describes how to install TinyTex, which is required to render LaTeX-style documents (i.e., PDFs) in R.

@sec-zotero provides tips on installing Zotero and related addins for integrations with R and RStudio. This is optional, and slightly more advanced.

# R and RStudio {#sec-R}

Download R [here](https://cran.r-project.org/).

Download RStudio [here](https://www.rstudio.com/products/rstudio/download/).

You must first download R, then RStudio.

## Packages {#sec-packages}

R packages available on the Comprehensive R Archive Network, commonly known as CRAN repository, can be easily installed using the command `isntall.packages("packageName")`. Some packages we will be needing are:

- `here` package
- `tidyverse` family of packages
  -  will automatically include packages we need, like `dplyr` and `ggplot2`
  
To download multiple packages at once, use the 'concatenate' function in r (`c()`) inside `install.packages()`:

```{r, eval = F}
install.packages(c("here", 
                   "tidyverse",
                   "pacman"))
```

## Developer packages (optional)

Not all packages are available on CRAN. You will need to install them directly through the developer. Two packages that are commonly used to do this are `remotes` and `devools`. Let's try installing `remotes` and then installing the `rbbt`, an addin for Zotero (discussed below).

```{r}
if(!requireNamespace("remotes", quietly = TRUE)) install.packages("remotes")
```

```{r}
remotes::install_github("paleolimbot/rbbt")
```

### `kableExtra` for table formatting

```{r}
remotes::install_github("haozhu233/kableExtra")
```

### `quartoExtra`

For dark/light mode toggle: https://debruine.github.io/quarto_demo/quartoExtra.html

```{r}
devtools::install_github("debruine/quartoExtra")
```


## RStudio Global options (optional)

Here are my preferred global options (RStudio > Tools > Global options). I highly recommend following the settings for 'workspace' and 'R Sessions' to ensure reproducibility. The other settings you can play around with to find what you like.

  - General > Basic
    - **Workspace** (for reproducibile workflow!!!)
        + uncheck 'Restore .RData into workshapce at startup'
        + Save workspace to .RData on exit: ***Never***
    - **R Sessions**
        + uncheck 'Restore previously open source documents at startup'
  - Code > Display
    - General
      + Show white space characters
      + Allow scroll past end of document
      + highlight selected line
  - Appearance
    - Editor theme: Cobalt

# tinyTex {#sec-tinytex}

To render PDF documents with LaTeX under the hood, we need to install [tinytex](https://yihui.org/tinytex/). There are different ways to do this:

- run the following in the *terminal*: `quarto install tinytex`
- or in the console: `tinytex::install_tinytex()`

# webR

In the Terminal:

```{r}
#| eval: false
quarto add coatless/quarto-webr
```

Then add WebR filter to slides in the YAML:

```{r}
#| eval: false
format: revealjs
filters:
  - webr
```

# timesaveR

e.g., for the `as_tribble()` function

```{r}
#| eval: false
remotes::install_github('lukaswallrich/timesaveR')
```

# glossary

https://debruine.github.io/glossary/

```{r}
install.packages("glossary")
```